from itsdangerous import TimedJSONWebSignatureSerializer
from django.conf import settings
# 6.使用 itsdangerous 加解密
# 1.导包 from itsdangerous import TimedJSONWebSignatureSerializer
# 2. 实例化 serializer = Serializer(secret_key='abc', expires_in=24 * 15 * 60)
# 3. 加密serializer.dumps() --注意:返回是 bytes
# 4. 解密serializer.loads()
from utils import constant


class Secret(object):
    def __init__(self):
        self.serializer = TimedJSONWebSignatureSerializer(secret_key=settings.SECRET_KEY, expires_in=constant.EMAIL_ACTIVE_AGE)

    def dumps(self, str: str, *args, **kwargs) -> str:
        return self.serializer.dumps(str,  *args, **kwargs).decode()

    def loads(self, str: str,  *args, **kwargs) -> str:
        return self.serializer.loads(str,  *args, **kwargs)
